﻿using System;
using BitBenderGames;
using UnityEngine;
using Yoozoo.Mars.Got;

namespace Yoozoo.Gameplay.City
{
    public class HUDScaleControl : MonoBehaviour
    {
        private bool inited = false;
        private Transform camTrans;
        private Vector3 scale = Vector3.one;
        [SerializeField]
        private Vector3 rotate = new Vector3(45, -10, 0);
        [Header("Hud最小scale 对应缩放最大")]
        public float minScale = 0.75f;
        [Header("Hud最大scale 对应缩放最小")]
        public float maxScale = 1.8f;
        [Header("是否不需要初始化")]
        public bool notNeedInit = false;
        private MobileTouchCamera mobileTouchCamera;
        private TouchCameraController touchCameraController;
        private float CamZoomMin, CamZoomMax, curCamZoom;
        private float camHight;
        private Vector3 initScale;
        private bool isSetInitScale;
        public bool IgnoreRotateX = false;

        public void SetInitScale(float x,float y, float z)
        {
            initScale = new Vector3(x,y,z);
            transform.localScale = initScale;
            isSetInitScale = true;
        }

        public void InitData()
        {
            camHight = 0;
        }

        // Start is called before the first frame update
        void Awake()
        {
            Debug.Log("HUDScaleLimit Awake" + transform.name);
            //进游戏前生成的需要camera数据初始化
            AddEventListeners();
            //hud会生成再删除，内城相关逻辑；进游戏后生成的收不到初始化事件
            OnInitCityCameraEnd();
            if (notNeedInit)
            {
                Vector3 tScale = transform.localScale;
                SetInitScale(tScale.x, tScale.y, tScale.z);
            }
        }

        private void AddEventListeners()
        {
            GTAGameStart.eventManager.AddEvent(ClientEvent.OnInitCityCameraEnd, OnInitCityCameraEnd);
        }
        private void RomoveEventListeners()
        {
            GTAGameStart.eventManager.RemoveEvent(ClientEvent.OnInitCityCameraEnd, OnInitCityCameraEnd);
        }
        
        //touchCameraController初始化结束接受到的回调
        void OnInitCityCameraEnd()
        {
            if (!CityManager.Instance)
            {
                return;
            }
            mobileTouchCamera = CityManager.Instance.mobileTouchCamera;
            if (mobileTouchCamera != null)
            {
                touchCameraController = mobileTouchCamera.GetComponent<TouchCameraController>();
                float zoomInit = touchCameraController.GetCameraZoomInit();
                // Debug.Log("HUDScaleLimit touchCameraController:" + zoomInit);
                if (zoomInit != 0)
                {
                    inited = true;
                    CamZoomMin = mobileTouchCamera.CamZoomMin;
                    CamZoomMax = mobileTouchCamera.CamZoomMax;
                    camTrans = mobileTouchCamera.Cam.transform;
                    //Debug.Log("HUDScaleLimit CamZoomMin:" + CamZoomMin + " CamZoomMax:" + CamZoomMax +" CamZoomInit:" + CamZoomInit);
                }
            }
        }

        private void OnEnable()
        {
            InitData();
            SetScale();
        }

        void Update()
        {
            SetScale();
        }

        private void SetScale()
        {
            
            if (inited && camTrans != null && (isSetInitScale || notNeedInit))
            {
                if(camHight == camTrans.localPosition.y) return;
                if (mobileTouchCamera)
                {
                    CamZoomMin = mobileTouchCamera.CamZoomMin;
                    CamZoomMax = mobileTouchCamera.CamZoomMax;
                }
                curCamZoom = camTrans.localPosition.y * 1.414f;
                float childScale = (curCamZoom - CamZoomMin) * (maxScale - minScale) / (CamZoomMax - CamZoomMin) +
                                   minScale;
                //保底兼容操作
                if (childScale < minScale)
                {
                    childScale = minScale;
                }
                else if (childScale > maxScale)
                {
                    childScale = maxScale;
                }
                scale.x = initScale.x * childScale;
                scale.y = initScale.y * childScale;
                transform.localScale = scale;

                if (!IgnoreRotateX)
                {
                    rotate.x = camTrans.localEulerAngles.x;
                    transform.localEulerAngles = rotate;
                }
                camHight = camTrans.localPosition.y;
            }
        }

        private void OnDestroy()
        {
            RomoveEventListeners();
        }
    }

}

